package org.xxpay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.AppUser;
import org.xxpay.core.entity.AppUserExample;
import org.xxpay.core.service.IAppUserService;
import org.xxpay.service.dao.mapper.AppUserMapper;

import java.util.List;

@Service
public class AppUserServiceImpl implements IAppUserService {

    @Autowired
    AppUserMapper appUserMapper;

    @Override
    public int add(AppUser appUser) {
        return appUserMapper.insertSelective(appUser);
    }

    @Override
    public int update(AppUser appUser) {
        return appUserMapper.updateByPrimaryKeySelective(appUser);
    }

    @Override
    public Integer count(AppUser appUser) {
        AppUserExample example = new AppUserExample();
        AppUserExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, appUser);
        return appUserMapper.countByExample(example);
    }

    @Override
    public AppUser findByUserId(Long userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public List<AppUser> select(int offset, int limit, AppUser appUser) {
        AppUserExample example = new AppUserExample();
        example.setOrderByClause("createTime desc");
        example.setOffset(offset);
        example.setLimit(limit);
        AppUserExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, appUser);
        return appUserMapper.selectByExample(example);
    }

    void setCriteria(AppUserExample.Criteria criteria, AppUser appUser) {
        if(appUser != null) {
            if(appUser.getUserId() != null) criteria.andUserIdEqualTo(appUser.getUserId());
            if(appUser.getAppStatus() != null && appUser.getAppStatus() != -99) criteria.andAppStatusEqualTo(appUser.getAppStatus());
            if(appUser.getStatus() != null && appUser.getStatus().byteValue() != -99) criteria.andStatusEqualTo(appUser.getStatus());
        }
    }
}
